package com.example.administrator.roundTimer;

import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private Button btnStart;
    private Button btnStop;
    private EditText txtSecond;
    private EditText txtTime;
    private TextView tvLastSecond;
    private TextView tvCurrentTime;
    private int testA = 1;
    private int lastSecond = 0;
    private int currentTime = 0;
    private CountDownTimer timer;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);
        txtSecond = (EditText)findViewById(R.id.txtSecond);
        txtTime = (EditText)findViewById(R.id.txtTime);
        tvLastSecond = (TextView)findViewById(R.id.tvLastSecond);
        tvCurrentTime = (TextView)findViewById(R.id.tvCurrentTime);
        btnStop.setEnabled(false);
    }

    public void btnStart_click(View view)
    {
        String second = txtSecond.getText().toString();
        String time = txtTime.getText().toString();
        if (second.equals("") || time.equals("")) {
            Toast.makeText(this, "请输入时长和次数", Toast.LENGTH_LONG).show();
            return;
        }
        int secondInt = Integer.parseInt(second);
        int timeInt = Integer.parseInt(time);
        if (secondInt <= 0 || timeInt <= 0) {
            Toast.makeText(this, "时长和次数必须大于0", Toast.LENGTH_LONG).show();
            return;
        }
        btnStart.setEnabled(false);
        btnStop.setEnabled(true);
        lastSecond = secondInt;
        currentTime = timeInt;
        tvLastSecond.setText(second);
        tvCurrentTime.setText(time);
        timer = new CountDownTimer((lastSecond + 1) * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                tvLastSecond.setText(String.valueOf(millisUntilFinished/1000));
            }
            @Override
            public void onFinish() {
                tvLastSecond.setText("0");
                currentTime--;
                if (currentTime > 0) {
                    tvLastSecond.setText(String.valueOf(lastSecond));
                    tvCurrentTime.setText(String.valueOf(currentTime));
                    timer.start();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                startAlarm("second");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                } else {
                    btnStart.setEnabled(true);
                    btnStop.setEnabled(false);
                    tvCurrentTime.setText("0");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                startAlarm("over");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        }.start();
        Toast.makeText(this, "开始倒计时", Toast.LENGTH_LONG).show();
    }

    public void btnStop_click(View view)
    {
        if (timer != null) {
            timer.cancel();
            btnStart.setEnabled(true);
            btnStop.setEnabled(false);
            tvLastSecond.setText("0");
            tvCurrentTime.setText("0");
        }
    }

    /**
     * 开启手机系统自带铃声
     */
    public void startAlarm(String flag) {
        if (flag.equals("over")) {
            mMediaPlayer = MediaPlayer.create(this, R.raw.time_over);
        } else {
            mMediaPlayer = MediaPlayer.create(this, R.raw.ring);
        }
        mMediaPlayer.setLooping(false);
        try {
            mMediaPlayer.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                stopAlarm();
            }
        });
        mMediaPlayer.start();
    }

    public void stopAlarm(){
        mMediaPlayer.stop();
    }

    /**
     * 监听back键并停止倒计时
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        btnStop_click(null);
    }

    /**
     * 获取系统自带铃声的uri
     * @return RingtoneManager.getActualDefaultRingtoneUri(this,
            RingtoneManager.TYPE_RINGTONE)
     */
    private Uri getSystemDefultRingtoneUri() {
        return RingtoneManager.getActualDefaultRingtoneUri(this,
                RingtoneManager.TYPE_RINGTONE);
    }
}
